package com.activitycode.activity.square;


import java.util.Map;

import com.activitycode.activity.find.EvalueActivity;
import com.activitycode.activity.square.PhotoDetailActivity.RefreshTask;
import com.activitycode.datamodel.ground.AddEvaluate;
import com.activitycode.datamodel.ground.AddEvaluateResult;
import com.activitycode.datamodel.ground.PhotoDetailResult;
import com.activitycode.net.HttpUtil;
import com.activitycode.net.Httpclient;
import com.activitycode.tools.EncryTools;
import com.activitycode.tools.PublicTools;
import com.activitycode.util.BaseTask;
import com.activitycode.util.CommConstant;
import com.activitycode.util.JacksonUtil;
import com.activitycode.util.CommConstant.TaskResult;
import com.activitycode.util.SharedPreferrenceManager;
import com.activitycode.view.CustomProgressDialog;
import com.duostec.zhuyue.pets.R;
import com.mining.app.zxing.decoding.Intents.Share;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class AddEvaluateActivity extends Activity {
	private TextView title_evalueate1_info_name,store_name,evaluate_score;
	private Button title_evaluate1_submit;
	private ImageButton title_evalueate1_info_back;
	private EditText comment;
	private RatingBar store_evaluate_ratingBar;
	private AddEvaluate info;
	private String bussinessId;
	private String user_id;
	private String content;
	private String comment_score;
	private String storeName;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_store_evaluate);
		initView();
		initData();

	}
	
	private void initView() {
		title_evalueate1_info_name = (TextView) this.findViewById(R.id.title_evalueate1_info_name);
		store_name = (TextView) this.findViewById(R.id.store_name);
		evaluate_score = (TextView) this.findViewById(R.id.evaluate_score);
		title_evaluate1_submit = (Button) this.findViewById(R.id.title_evaluate1_submit);
		comment = (EditText) this.findViewById(R.id.comment);
		store_evaluate_ratingBar = (RatingBar) this.findViewById(R.id.store_evaluate_ratingBar);
		title_evalueate1_info_back = (ImageButton) this.findViewById(R.id.title_evalueate1_info_back);
		
		title_evalueate1_info_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
				PublicTools.closeKeyBoard(AddEvaluateActivity.this);
			}
		});
		title_evaluate1_submit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				submit();
				
			}
		});
		store_evaluate_ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
			
			@Override
			public void onRatingChanged(RatingBar arg0, float arg1, boolean arg2) {
				// TODO Auto-generated method stub
				evaluate_score.setText(""+arg1);
			}
		});
		// TODO Auto-generated method stub
		
	}
	
	private void initData() {
		// TODO Auto-generated method stub
		bussinessId = getIntent().getStringExtra("business_id");
		user_id = SharedPreferrenceManager.getUserId(getApplicationContext());
		storeName = getIntent().getStringExtra("storeName");
		store_name.setText(storeName);

	}
	
	
	private void submit(){
		PublicTools.closeKeyBoard(this);
		if(HttpUtil.isnet(this)){
			
			content = comment.getText().toString();
			//comment_score = store_evaluate_ratingBar.getRating();
			comment_score = (String) evaluate_score.getText();
			Log.d("comment_score", comment_score);
			if(content.isEmpty()){
				Toast.makeText(this, 
						getResources().getString(R.string.error_item2), Toast.LENGTH_SHORT).show();
				return;
			}
			
			String url = "";			
			url = CommConstant.SERVICE_HOST_IP_LAN+CommConstant.ADDEVALUATE;						
			Map<String, String> arg = EncryTools.getEncryMap();	
			
			
			arg.put("business_id", ""+bussinessId);
			arg.put("user_id", ""+user_id);
			arg.put("content", ""+content);
			arg.put("score", ""+comment_score);
				
			
			SubmitTask task = new SubmitTask(this,url,arg);
			task.execute();
		}else{
			Toast.makeText(this, 
					getResources().getString(R.string.error_item1), Toast.LENGTH_SHORT).show();
			
		}
	}
	
	public class SubmitTask extends BaseTask<Void, String, TaskResult>{
		public static final String TAG = "RefreshTask";
		private String httpUrl;
		private Map<String, String> argList;
		private CustomProgressDialog progress=null;
		private Context context;
		private String msg = "";
		
		public SubmitTask(Context ct ,String url, Map<String, String> arg){
			this.httpUrl = url;
			this.argList = arg;
			this.context = ct;
		}
		
		@Override
		public void onPreExecute() 
		{
			if(progress==null){
				showPD(context);
			}
			
		}

		@Override
		public TaskResult doInBackground(Void... params) {
			// TODO Auto-generated method stub
			TaskResult taskResult = TaskResult.ACRIVATE;
			String result = Httpclient.postData(httpUrl, argList);						
			JacksonUtil json = JacksonUtil.getInstance();			
			AddEvaluateResult res = json.readValue(result, AddEvaluateResult.class);
			if(res==null){
				
				taskResult = TaskResult.CANCELLED;
			}else{
				
				
				
				if(res.result){
					taskResult = TaskResult.OK;
					
				}else{
					
					taskResult = TaskResult.ERROR;
				}
			}
			return taskResult;
		}
		
		@Override
		public void onPostExecute(TaskResult result) 
		{
			if(progress!=null){
				progress.dismiss();
			}
			switch(result){
			
				case OK:
				
					finishResult();
					break;
				case ERROR:					
					Toast.makeText(context, context.getResources().getString(R.string.evaluate_flase), Toast.LENGTH_SHORT).show();
					break;
				case CANCELLED:					
//					Toast.makeText(getApplicationContext(), ""+msg, Toast.LENGTH_SHORT).show();
					break;
				default:
					break;
			}
		}
		protected void showPD(Context paramContext)
		  {
			progress = CustomProgressDialog.createDialog(paramContext);
		     progress.setMessage(context.getString(R.string.uploading));
		     progress.setCanceledOnTouchOutside(false);
		     progress.show();
		  }
	}
	
	
	private void finishResult(){
		Intent intent = new Intent(AddEvaluateActivity.this,EvalueActivity.class);
		setResult(RESULT_OK, intent);
		finish();
	}


}
